Telegram Group & Telegram Channel
🐍 7 “бесполезных” функций Python, которые на самом деле полезны

Инструменты из стандартной библиотеки, которые могут удивить:

1. textwrap.dedent() — убирает отступы у многострочного текста.

import textwrap
text = textwrap.dedent(\"\"\"
Привет!
Это текст с отступами.
\"\"\").strip()
print(text)

2. difflib.get_close_matches() — находит схожие строки.

import difflib
words = ["python", "java", "javascript"]
print(difflib.get_close_matches("javascrip", words))

3. uuid.uuid4() — создает уникальный идентификатор.

import uuid
print(uuid.uuid4())

4. shutil.get_terminal_size() — определяет размеры терминала.

import shutil
columns, rows = shutil.get_terminal_size()
print(f"Размер терминала: {columns}x{rows}")

5. functools.lru_cache() — сохраняет результаты функции в кэше.

from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
print(fib(100))

6. itertools.groupby() — группирует элементы по ключу.

from itertools import groupby
data = [(&apos;fruit&apos;, &apos;apple&apos;), (&apos;fruit&apos;, &apos;banana&apos;), (&apos;veg&apos;, &apos;carrot&apos;)]
for key, group in groupby(data, lambda x: x[0]):
print(key, list(group))

7. contextlib.suppress() — элегантная альтернатива конструкции try-except.

from contextlib import suppress
with suppress(FileNotFoundError):
open("not_exist.txt")

@Python_Community_ru



tg-me.com/Python_Community_ru/2569
Create:
Last Update:

🐍 7 “бесполезных” функций Python, которые на самом деле полезны

Инструменты из стандартной библиотеки, которые могут удивить:

1. textwrap.dedent() — убирает отступы у многострочного текста.

import textwrap
text = textwrap.dedent(\"\"\"
Привет!
Это текст с отступами.
\"\"\").strip()
print(text)

2. difflib.get_close_matches() — находит схожие строки.

import difflib
words = ["python", "java", "javascript"]
print(difflib.get_close_matches("javascrip", words))

3. uuid.uuid4() — создает уникальный идентификатор.

import uuid
print(uuid.uuid4())

4. shutil.get_terminal_size() — определяет размеры терминала.

import shutil
columns, rows = shutil.get_terminal_size()
print(f"Размер терминала: {columns}x{rows}")

5. functools.lru_cache() — сохраняет результаты функции в кэше.

from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
print(fib(100))

6. itertools.groupby() — группирует элементы по ключу.

from itertools import groupby
data = [(&apos;fruit&apos;, &apos;apple&apos;), (&apos;fruit&apos;, &apos;banana&apos;), (&apos;veg&apos;, &apos;carrot&apos;)]
for key, group in groupby(data, lambda x: x[0]):
print(key, list(group))

7. contextlib.suppress() — элегантная альтернатива конструкции try-except.

from contextlib import suppress
with suppress(FileNotFoundError):
open("not_exist.txt")

@Python_Community_ru

BY Python Community


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/Python_Community_ru/2569

View MORE
Open in Telegram


Python Community Telegram | DID YOU KNOW?

Date: |

Export WhatsApp stickers to Telegram on Android

From the Files app, scroll down to Internal storage, and tap on WhatsApp. Once you’re there, go to Media and then WhatsApp Stickers. Don’t be surprised if you find a large number of files in that folder—it holds your personal collection of stickers and every one you’ve ever received. Even the bad ones.Tap the three dots in the top right corner of your screen to Select all. If you want to trim the fat and grab only the best of the best, this is the perfect time to do so: choose the ones you want to export by long-pressing one file to activate selection mode, and then tapping on the rest. Once you’re done, hit the Share button (that “less than”-like symbol at the top of your screen). If you have a big collection—more than 500 stickers, for example—it’s possible that nothing will happen when you tap the Share button. Be patient—your phone’s just struggling with a heavy load.On the menu that pops from the bottom of the screen, choose Telegram, and then select the chat named Saved messages. This is a chat only you can see, and it will serve as your sticker bank. Unlike WhatsApp, Telegram doesn’t store your favorite stickers in a quick-access reservoir right beside the typing field, but you’ll be able to snatch them out of your Saved messages chat and forward them to any of your Telegram contacts. This also means you won’t have a quick way to save incoming stickers like you did on WhatsApp, so you’ll have to forward them from one chat to the other.

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

Python Community from us


Telegram Python Community
FROM USA